<?php

namespace app\devices\controller;

use app\devices\service\SocketService;
use app\wechat\service\WechatService;
use think\admin\Controller;
use think\admin\Storage;
use think\cache\driver\Redis;
use think\exception\HttpResponseException;
use WeMini\Qrcode;

/**
 * 开箱日志
 * Class Index
 * @package app\data\controller
 */
class Locklog extends Controller
{

    /**
     * 绑定数据表
     * @var string
     */
    private $table = 'JcgOplog';

    /**
     * 开箱日志
     * @auth true
     * @menu true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function index()
    {
        $this->title = '开箱日志';
        $map = [];

        $query = $this->_query($this->table);
        $query->like('content,username');
        $query->where($map)->order('id desc')->equal('id,order_id,box_id,user_id')->dateBetween('create_at')->page();
    }

    /**
     * 列表数据处理
     * @param $data
     */
    public function _index_page_filter(&$data){
        if($data){
            $orders = $this->app->db->name("jcg_devices_order")->where("id","in",array_column($data,'order_id'))->column("order_no, extends","id");

            $boxs = $this->app->db->name("jcg_boxs")->alias('b')->leftJoin("jcg_devices d",'d.id=b.d_id')->where("b.id","in",array_column($data,'box_id'))->field("b.id, box_num,device_name")->select()->toArray();

            if($boxs){
                $boxs = array_column($boxs, null,'id');
            }

            foreach ($data as &$row){
                $box = $boxs[$row['box_id']]??[];
                $row['box'] = $box?$box['device_name'].' 的 '.$box['box_num'].' 柜门 - '.$row['box_id']:"";
                $row['order'] = isset($orders[$row['order_id']]['order_no'])?$orders[$row['order_id']]['order_no']." - ".$row['order_id']:'';
            }
        }
    }

    /**
     * 编辑开箱日志
     * @auth true
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 数据表单处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    protected function _form_filter(array &$data)
    {

    }

    /**
     * 修改开箱日志状态
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function state()
    {
        $this->_save($this->table, $this->_vali([
            'status.in:1,2'  => '状态值范围异常！',
            'status.require' => '状态值不能为空！',
        ]));
    }

    /**
     * 删除开箱日志
     * @auth true
     * @throws \think\db\exception\DbException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }
}


